﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SocialMediaEntryViewModel.cs" company="Catel development team">
//   Copyright (c) 2008 - 2012 Catel development team. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Catwitter.ViewModels
{
    using System;
    using Catel;
    using Catel.Data;
    using Catel.MVVM;
    using Catel.MVVM.Services;
    using Models;
    using Windows.UI.Xaml.Media.Imaging;

    /// <summary>
    /// SocialMediaEntry view model.
    /// </summary>
    public class SocialMediaEntryViewModel : ViewModelBase
    {
        #region Constructor & destructor
        /// <summary>
        /// Initializes a new instance of the <see cref="SocialMediaEntryViewModel"/> class.
        /// </summary>
        /// <param name="socialMediaEntry">The social media entry.</param>
        /// <exception cref="ArgumentNullException">The <param name="socialMediaEntry"/> is <c>null</c>.</exception>
        public SocialMediaEntryViewModel(ISocialMediaEntry socialMediaEntry)
        {
            Argument.IsNotNull("socialMediaEntry", socialMediaEntry);

            SocialMediaEntry = socialMediaEntry;

            if (socialMediaEntry.Photo != null)
            {
                //Photo = socialMediaEntry.Photo.ConvertBitmapToBitmapSource();
            }

            OpenLink = new Command<object, object>(OnOpenLinkExecute, OnOpenLinkCanExecute);
        }
        #endregion

        #region Properties
        /// <summary>
        /// Gets the title of the view model.
        /// </summary>
        /// <value>The title.</value>
        public override string Title
        {
            get { return "Social Media Entry"; }
        }
        #endregion

        #region Models
        #region Properties
        /// <summary>
        /// Gets or sets the social media entry.
        /// </summary>
        [Model]
        private ISocialMediaEntry SocialMediaEntry { get; set; }
        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the photo.
        /// </summary>
        /// <remarks>
        /// This is not an automatic ViewModelToModel property mapping because we need to convert a <see cref="System.Drawing.Bitmap"/>
        /// into a <see cref="BitmapSource"/>.
        /// </remarks>
        public BitmapSource Photo { get; set; }

        /// <summary>
        /// Gets or sets the message.
        /// </summary>
        [ViewModelToModel("SocialMediaEntry")]
        public string Message { get; set; }

        /// <summary>
        /// Gets or sets the message preview.
        /// </summary>
        [ViewModelToModel("SocialMediaEntry")]
        public string MessagePreview { get; set; }

        /// <summary>
        /// Gets or sets the author.
        /// </summary>
        [ViewModelToModel("SocialMediaEntry")]
        public string Author { get; set; }

        /// <summary>
        /// Gets or sets the timestamp.
        /// </summary>
        [ViewModelToModel("SocialMediaEntry")]
        public DateTime Timestamp { get; set; }

        /// <summary>
        /// Gets or sets the url.
        /// </summary>
        [ViewModelToModel("SocialMediaEntry")]
        public string Url { get; set; }

        /// <summary>
        /// Gets or sets the provider name.
        /// </summary>
        /// <remarks>
        /// Notice that thanks to the additional "Provider" provided for the attribute, the view model automatically
        /// maps ViewModel.ProviderName to SocialMediaEntry.Provider.
        /// </remarks>
        [ViewModelToModel("SocialMediaEntry", "Provider")]
        public string ProviderName { get; set; }
        #endregion

        #endregion

        #region Commands
        /// <summary>
        /// Gets the OpenLink command.
        /// </summary>
        public Command<object, object> OpenLink { get; private set; }

                /// <summary>
        /// Method to check whether the OpenLink command can be executed.
        /// </summary>
        /// <param name="parameter">The parameter of the command.</param>
        private bool OnOpenLinkCanExecute(object parameter)
        {
            return !string.IsNullOrEmpty(Url);
        }

        /// <summary>
        /// Method to invoke when the OpenLink command is executed.
        /// </summary>
        /// <param name="parameter">The parameter of the command.</param>
        private void OnOpenLinkExecute(object parameter)
        {
            var processService = GetService<IProcessService>();
            processService.StartProcess(Url);
        }
        #endregion
    }
}